package bjfuwjx.exp3;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.SimpleBindings;
import javax.swing.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.io.File;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Map;
import java.util.TreeMap;

public class ConfigurableDrawing {
    static MyPanel mp = new MyPanel();
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setSize(1500, 1000);
        frame.setTitle("简易几何画板");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel left = new JPanel();
        JPanel right = new JPanel();
        JSplitPane fg = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, left, right);
        fg.setDividerLocation(1200);
        frame.add(fg);
        GridBagLayout layout = new GridBagLayout();
        right.setLayout(layout);
        left.setLayout(layout);
        JButton bt1 = new JButton("绘图");
        JButton bt2 = new JButton("配置样例");
        JTextArea jta = new JTextArea();
        JScrollPane jsp = new JScrollPane(jta);
        jta.append("样例配置信息如下：\n" +
                "<!--\n" +
                "【1】配置文件用xml格式，注释采用标准的xml注释格式。\n" +
                "【2】有8类设定：\n" +
                "（1）<bg>：用于设定画布的背景颜色及横向和纵向坐标范围；\n" +
                "（2）<points>：用于画一个或多个点，可以对点的颜色和填充等进行配置；\n" +
                "（3）<lines>：用于画折线，可以对折线的颜色和粗细进行配置：\n" +
                "（4）<line>：根据提供的单个点和斜率，画射线；\n" +
                "（5）<curve>：用于画曲线，画出采用公式描述的曲线；\n" +
                "（6）<shape>：用于画矩形和椭圆，能够对填充颜色进行配置；\n" +
                "（7）<xScale>：用于画x轴刻度；\n" +
                "（8）<yScale>：用于画y轴刻度。\n" +
                "【3】与颜色相关的标签中，均可以采用两种方式之一设定：\n" +
                "（1）用名称颜色，名称颜色共10种：\n" +
                "black, blue, cyan, gray, green, \n" +
                "orange, pink, red, white, yellow\n" +
                "（2）用RGB颜色，例如：\n" +
                "255.0.155\n" +
                "\n" +
                "【4】其它设定包括：\n" +
                "（1）所有与线的粗细有关的设定都默认为1；\n" +
                "（2）所有与颜色相关的设定的默认值都为黑色（0,0,0)；背景为白色；\n" +
                "（3）画曲线时，默认离散点的数量为500；\n" +
                "（4）画刻度时，默认间隔数量<amount>为10.\n" +
                "\n" +
                "【5】以下为各种设置示例：\n" +
                "-->\n" +
                "\n" +
                "<bg>\n" +
                "<col> 255,255,255 </col>\n" +
                "<xRange> -0.1,1.1 </xRange>\n" +
                "<yRange> -0.1,1.1 </yRange>\n" +
                "</bg>\n" +
                "\n" +
                "<points>\n" +
                "<pad> true </pad>\n" +
                "<col> red </col>\n" +
                "<wid> 2 </wid>\n" +
                "<radius> 6 </radius>\n" +
                "<list>\n" +
                "0.2,0.2\n" +
                "0.2,0.5\n" +
                "0.2,0.8\n" +
                "0.5,0.8\n" +
                "0.8,0.8\n" +
                "0.8,0.5\n" +
                "0.8,0.2\n" +
                "0.5,0.2\n" +
                "</list>\n" +
                "</points>\n" +
                "\n" +
                "<lines>\n" +
                "<col> orange </col>\n" +
                "<wid> 3 </wid>\n" +
                "<list>\n" +
                "0.2,0.2\n" +
                "0.2,0.5\n" +
                "0.2,0.8\n" +
                "0.5,0.8\n" +
                "0.8,0.8\n" +
                "0.8,0.5\n" +
                "0.8,0.2\n" +
                "0.5,0.2\n" +
                "0.5,0.5\n" +
                "</list>\n" +
                "</lines>\n" +
                "\n" +
                "<line>\n" +
                "<col>  </col>\n" +
                "<wid> 1 </wid>\n" +
                "<point> 0.3,0.3 </point>\n" +
                "<slope> inf </slope>\n" +
                "</line>\n" +
                "\n" +
                "<curve>\n" +
                "<col> 0,0,255 </col>\n" +
                "<wid> 3 </wid>\n" +
                "<range> 0,1 </range>\n" +
                "<amount> 500 </amount>\n" +
                "<function> Math.pow(x,2)-0.05 </function>\n" +
                "</curve>\n" +
                "\n" +
                "<shape>\n" +
                "<col> 180,180,180 </col>\n" +
                "<wid> 1 </wid>\n" +
                "<type> oval </type>\n" +
                "<!-- type includes oval & rect -->\n" +
                "<pad> true </pad>\n" +
                "<center> 0.6,0.8 </center>\n" +
                "<width> 0.3 </width>\n" +
                "<height> 0.2 </height>\n" +
                "</shape>\n" +
                "\n" +
                "<shape>\n" +
                "<col> 180,180,180 </col>\n" +
                "<wid> 5 </wid>\n" +
                "<type> rect </type>\n" +
                "<!-- type includes oval & rect -->\n" +
                "<pad> false </pad>\n" +
                "<center> 0.3,0.4 </center>\n" +
                "<width> 0.3 </width>\n" +
                "<height> 0.2 </height>\n" +
                "</shape>\n" +
                "\n" +
                "<scale>\n" +
                "<col> black </col>\n" +
                "<wid> 1 </wid>\n" +
                "<direction> x </direction>\n" +
                "<pos> 0.0 </pos>\n" +
                "<from> 0 </from>\n" +
                "<step> 0.1 </step>\n" +
                "<amount> 10 </amount>\n" +
                "<precision> 1 </precision>\n" +
                "</scale>\n" +
                "\n" +
                "<scale>\n" +
                "<col> black </col>\n" +
                "<wid> 1 </wid>\n" +
                "<direction> y </direction>\n" +
                "<pos> 0 </pos>\n" +
                "<from> 0 </from>\n" +
                "<step> 0.1 </step>\n" +
                "<amount> 10 </amount>\n" +
                "<precision> 1 </precision>\n" +
                "</scale>\n");
        jsp.setVisible(false);
        JPanel r_empty = new JPanel();
        JPanel l_empty = new JPanel();
        l_empty.setVisible(true);
        r_empty.setVisible(true);
        right.add(bt1);
        right.add(bt2);
        right.add(jsp);
        right.add(r_empty);
        left.add(l_empty);
        GridBagConstraints s = new GridBagConstraints();
        s.fill = GridBagConstraints.BOTH;
        s.gridwidth = 1;
        s.weightx = 1;
        s.weighty = 0;
        layout.setConstraints(bt1, s);
        s.gridwidth = 0;
        s.weightx = 1;
        s.weighty = 0;
        layout.setConstraints(bt2, s);
        s.gridwidth = 0;
        s.weightx = 1;
        s.weighty = 1;
        layout.setConstraints(jsp, s);
        s.gridwidth = 0;
        s.weightx = 1;
        s.weighty = 1;
        layout.setConstraints(r_empty, s);
        s.gridwidth=0;
        s.weightx=1;
        s.weighty=1;
        layout.setConstraints(l_empty, s);
        bt2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jsp.setVisible(true);
                r_empty.setVisible(false);
            }
        });

        s.gridwidth=0;
        s.weightx=1;
        s.weighty=1;
        layout.setConstraints(mp, s);
        left.add(mp);
        mp.setVisible(false);
        bt1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                l_empty.setVisible(false);
                txt_update(jta);
                read_xml();
                mp.repaint();
                mp.setVisible(true);
            }
        });
        frame.setVisible(true);
    }
    public static void txt_update(JTextArea jta) {
        String contents=jta.getText();
        try {
            DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
            DocumentBuilder db=dbFactory.newDocumentBuilder();
            Document document=db.newDocument();
            int i=567;
            Element root=document.createElement("example");
            Element bg=document.createElement("bg");
            Element col_bg=document.createElement("col");
            String t="";
            i+=10;
            for (;;i++)
            {
                if (contents.substring(i,i+6).compareTo("</col>")==0)
                    break;
                t+=contents.charAt(i);
            }
            col_bg.setTextContent(t);
            bg.appendChild(col_bg);
            Element xRange=document.createElement("xRange");
            t="";
            i+=15;
            for (;;i++)
            {
                if (contents.substring(i,i+9).compareTo("</xRange>")==0)
                    break;
                t+=contents.charAt(i);
            }
            xRange.setTextContent(t);
            bg.appendChild(xRange);
            Element yRange=document.createElement("yRange");
            t="";
            i+=18;
            for (;;i++)
            {
                if (contents.substring(i,i+9).compareTo("</yRange>")==0)
                    break;
                t+=contents.charAt(i);
            }
            yRange.setTextContent(t);
            bg.appendChild(yRange);
            root.appendChild(bg);

            Element points=document.createElement("points");
            Element pad_points=document.createElement("pad");
            t="";
            i+=31;
            for (;;i++)
            {
                if (contents.substring(i,i+6).compareTo("</pad>")==0)
                    break;
                t+=contents.charAt(i);
            }
            pad_points.setTextContent(t);
            points.appendChild(pad_points);
            Element col_points=document.createElement("col");
            t="";
            i+=12;
            for (;;i++)
            {
                if (contents.substring(i,i+6).compareTo("</col>")==0)
                    break;
                t+=contents.charAt(i);
            }
            col_points.setTextContent(t);
            points.appendChild(col_points);
            Element wid_points=document.createElement("wid");
            t="";
            i+=12;
            for (;;i++)
            {
                if (contents.substring(i,i+6).compareTo("</wid>")==0)
                    break;
                t+=contents.charAt(i);
            }
            wid_points.setTextContent(t);
            points.appendChild(wid_points);
            Element radius_points=document.createElement("radius");
            t="";
            i+=15;
            for (;;i++)
            {
                if (contents.substring(i,i+9).compareTo("</radius>")==0)
                    break;
                t+=contents.charAt(i);
            }
            radius_points.setTextContent(t);
            points.appendChild(radius_points);
            Element list_points=document.createElement("list");
            t="";
            i+=16;
            for (;;i++)
            {
                if (contents.substring(i,i+7).compareTo("</list>")==0)
                    break;
                t+=contents.charAt(i);
            }
            list_points.setTextContent(t);
            points.appendChild(list_points);
            root.appendChild(points);

            Element lines=document.createElement("lines");
            Element col_lines=document.createElement("col");
            t="";
            i+=32;
            for (;;i++)
            {
                if (contents.substring(i,i+6).compareTo("</col>")==0)
                    break;
                t+=contents.charAt(i);
            }
            col_lines.setTextContent(t);
            lines.appendChild(col_lines);
            Element wid_lines=document.createElement("wid");
            t="";
            i+=12;
            for (;;i++)
            {
                if (contents.substring(i,i+6).compareTo("</wid>")==0)
                    break;
                t+=contents.charAt(i);
            }
            wid_lines.setTextContent(t);
            lines.appendChild(wid_lines);
            Element list_lines=document.createElement("list");
            t="";
            i+=13;
            for (;;i++)
            {
                if (contents.substring(i,i+7).compareTo("</list>")==0)
                    break;
                t+=contents.charAt(i);
            }
            list_lines.setTextContent(t);
            lines.appendChild(list_lines);
            root.appendChild(lines);

            Element line=document.createElement("line");
            Element col_line=document.createElement("col");
            t="";
            i+=30;
            for(;;i++)
            {
                if (contents.substring(i,i+6).compareTo("</col>")==0)
                    break;
                t+=contents.charAt(i);
            }
            col_line.setTextContent(t);
            line.appendChild(col_line);
            Element wid_line=document.createElement("wid");
            t="";
            i+=12;
            for(;;i++)
            {
                if (contents.substring(i,i+6).compareTo("</wid>")==0)
                    break;
                t+=contents.charAt(i);
            }
            wid_line.setTextContent(t);
            line.appendChild(wid_line);
            Element point_line=document.createElement("point");
            t="";
            i+=14;
            for(;;i++)
            {
                if (contents.substring(i,i+8).compareTo("</point>")==0)
                    break;
                t+=contents.charAt(i);
            }
            point_line.setTextContent(t);
            line.appendChild(point_line);
            Element slope_line=document.createElement("slope");
            t="";
            i+=16;
            for(;;i++)
            {
                if (contents.substring(i,i+8).compareTo("</slope>")==0)
                    break;
                t+=contents.charAt(i);
            }
            slope_line.setTextContent(t);
            line.appendChild(slope_line);
            root.appendChild(line);

            Element curve=document.createElement("curve");
            Element col_curve=document.createElement("col");
            t="";
            i+=31;
            for(;;i++)
            {
                if (contents.substring(i,i+6).compareTo("</col>")==0)
                    break;
                t+=contents.charAt(i);
            }
            col_curve.setTextContent(t);
            curve.appendChild(col_curve);
            Element wid_curve=document.createElement("wid");
            t="";
            i+=12;
            for(;;i++)
            {
                if (contents.substring(i,i+6).compareTo("</wid>")==0)
                    break;
                t+=contents.charAt(i);
            }
            wid_curve.setTextContent(t);
            curve.appendChild(wid_curve);
            Element range_curve=document.createElement("range");
            t="";
            i+=14;
            for(;;i++)
            {
                if (contents.substring(i,i+8).compareTo("</range>")==0)
                    break;
                t+=contents.charAt(i);
            }
            range_curve.setTextContent(t);
            curve.appendChild(range_curve);
            Element amount_curve=document.createElement("amount");
            t="";
            i+=17;
            for(;;i++)
            {
                if (contents.substring(i,i+9).compareTo("</amount>")==0)
                    break;
                t+=contents.charAt(i);
            }
            amount_curve.setTextContent(t);
            curve.appendChild(amount_curve);
            Element function_curve=document.createElement("function");
            t="";
            i+=21;
            for(;;i++)
            {
                if (contents.substring(i,i+11).compareTo("</function>")==0)
                    break;
                t+=contents.charAt(i);
            }
            function_curve.setTextContent(t);
            curve.appendChild(function_curve);
            root.appendChild(curve);

            Element shape_oval=document.createElement("shape");
            Element col_shape_oval=document.createElement("col_oval");
            t="";
            i+=35;
            for (;;i++)
            {
                if (contents.substring(i,i+6).compareTo("</col>")==0)
                    break;
                t+=contents.charAt(i);
            }
            col_shape_oval.setTextContent(t);
            shape_oval.appendChild(col_shape_oval);
            Element wid_shape_oval=document.createElement("wid_oval");
            t="";
            i+=12;
            for(;;i++)
            {
                if (contents.substring(i,i+6).compareTo("</wid>")==0)
                    break;
                t+=contents.charAt(i);
            }
            wid_shape_oval.setTextContent(t);
            shape_oval.appendChild(wid_shape_oval);
            Element type_shape_oval=document.createElement("type_oval");
            t="";
            i+=13;
            for(;;i++)
            {
                if (contents.substring(i,i+7).compareTo("</type>")==0)
                    break;
                t+=contents.charAt(i);
            }
            type_shape_oval.setTextContent(t);
            shape_oval.appendChild(type_shape_oval);
            Element pad_shape_oval=document.createElement("pad_oval");
            t="";
            String ss="<!-- type includes oval & rect -->";
            i+=ss.length();
            i+=14;
            for(;;i++)
            {
                if (contents.substring(i,i+6).compareTo("</pad>")==0)
                    break;
                t+=contents.charAt(i);
            }
            pad_shape_oval.setTextContent(t);
            shape_oval.appendChild(pad_shape_oval);
            Element center_shape_oval=document.createElement("center_oval");
            t="";
            i+=15;
            for(;;i++)
            {
                if (contents.substring(i,i+9).compareTo("</center>")==0)
                    break;
                t+=contents.charAt(i);
            }
            center_shape_oval.setTextContent(t);
            shape_oval.appendChild(center_shape_oval);
            Element width_shape_oval=document.createElement("width_oval");
            t="";
            i+=17;
            for(;;i++)
            {
                if (contents.substring(i,i+8).compareTo("</width>")==0)
                    break;
                t+=contents.charAt(i);
            }
            width_shape_oval.setTextContent(t);
            shape_oval.appendChild(width_shape_oval);
            Element height_shape_oval=document.createElement("height_oval");
            t="";
            i+=17;
            for(;;i++)
            {
                if (contents.substring(i,i+9).compareTo("</height>")==0)
                    break;
                t+=contents.charAt(i);
            }
            height_shape_oval.setTextContent(t);
            shape_oval.appendChild(height_shape_oval);
            root.appendChild(shape_oval);

            Element shape_rect=document.createElement("shape");
            Element col_shape_rect=document.createElement("col_rect");
            t="";
            i+=33;
            for (;;i++)
            {
                if (contents.substring(i,i+6).compareTo("</col>")==0)
                    break;
                t+=contents.charAt(i);
            }
            col_shape_rect.setTextContent(t);
            shape_rect.appendChild(col_shape_rect);
            Element wid_shape_rect=document.createElement("wid_rect");
            t="";
            i+=12;
            for(;;i++)
            {
                if (contents.substring(i,i+6).compareTo("</wid>")==0)
                    break;
                t+=contents.charAt(i);
            }
            wid_shape_rect.setTextContent(t);
            shape_rect.appendChild(wid_shape_rect);
            Element type_shape_rect=document.createElement("type_rect");
            t="";
            i+=13;
            for(;;i++)
            {
                if (contents.substring(i,i+7).compareTo("</type>")==0)
                    break;
                t+=contents.charAt(i);
            }
            type_shape_rect.setTextContent(t);
            shape_rect.appendChild(type_shape_rect);
            Element pad_shape_rect=document.createElement("pad_rect");
            t="";
            i+=ss.length();
            i+=14;
            for(;;i++)
            {
                if (contents.substring(i,i+6).compareTo("</pad>")==0)
                    break;
                t+=contents.charAt(i);
            }
            pad_shape_rect.setTextContent(t);
            shape_rect.appendChild(pad_shape_rect);
            Element center_shape_rect=document.createElement("center_rect");
            t="";
            i+=15;
            for(;;i++)
            {
                if (contents.substring(i,i+9).compareTo("</center>")==0)
                    break;
                t+=contents.charAt(i);
            }
            center_shape_rect.setTextContent(t);
            shape_rect.appendChild(center_shape_rect);
            Element width_shape_rect=document.createElement("width_rect");
            t="";
            i+=17;
            for(;;i++)
            {
                if (contents.substring(i,i+8).compareTo("</width>")==0)
                    break;
                t+=contents.charAt(i);
            }
            width_shape_rect.setTextContent(t);
            shape_rect.appendChild(width_shape_rect);
            Element height_shape_rect=document.createElement("height_rect");
            t="";
            i+=17;
            for(;;i++)
            {
                if (contents.substring(i,i+9).compareTo("</height>")==0)
                    break;
                t+=contents.charAt(i);
            }
            height_shape_rect.setTextContent(t);
            shape_rect.appendChild(height_shape_rect);
            root.appendChild(shape_rect);

            Element x_scale=document.createElement("scale");
            Element col_x_scale=document.createElement("col_x_scale");
            t="";
            i+=33;
            for (;;i++)
            {
                if (contents.substring(i,i+6).compareTo("</col>")==0)
                    break;
                t+=contents.charAt(i);
            }
            col_x_scale.setTextContent(t);
            x_scale.appendChild(col_x_scale);
            Element wid_x_scale=document.createElement("wid_x_scale");
            t="";
            i+=12;
            for (;;i++)
            {
                if (contents.substring(i,i+6).compareTo("</wid>")==0)
                    break;
                t+=contents.charAt(i);
            }
            wid_x_scale.setTextContent(t);
            x_scale.appendChild(wid_x_scale);
            Element direction_x_scale=document.createElement("direction_x_scale");
            t="";
            i+=18;
            for (;;i++)
            {
                if (contents.substring(i,i+12).compareTo("</direction>")==0)
                    break;
                t+=contents.charAt(i);
            }
            direction_x_scale.setTextContent(t);
            x_scale.appendChild(direction_x_scale);
            Element pos_x_scale=document.createElement("pos_x_scale");
            t="";
            i+=18;
            for (;;i++)
            {
                if (contents.substring(i,i+6).compareTo("</pos>")==0)
                    break;
                t+=contents.charAt(i);
            }
            pos_x_scale.setTextContent(t);
            x_scale.appendChild(pos_x_scale);
            Element from_x_scale=document.createElement("from_x_scale");
            t="";
            i+=13;
            for (;;i++)
            {
                if (contents.substring(i,i+7).compareTo("</from>")==0)
                    break;
                t+=contents.charAt(i);
            }
            from_x_scale.setTextContent(t);
            x_scale.appendChild(from_x_scale);
            Element step_x_scale=document.createElement("step_x_scale");
            t="";
            i+=14;
            for (;;i++)
            {
                if (contents.substring(i,i+7).compareTo("</step>")==0)
                    break;
                t+=contents.charAt(i);
            }
            step_x_scale.setTextContent(t);
            x_scale.appendChild(step_x_scale);
            Element amount_x_scale=document.createElement("amount_x_scale");
            t="";
            i+=16;
            for (;;i++)
            {
                if (contents.substring(i,i+9).compareTo("</amount>")==0)
                    break;
                t+=contents.charAt(i);
            }
            amount_x_scale.setTextContent(t);
            x_scale.appendChild(amount_x_scale);
            Element precision_x_scale=document.createElement("precision_x_scale");
            t="";
            i+=21;
            for (;;i++)
            {
                if (contents.substring(i,i+12).compareTo("</precision>")==0)
                    break;
                t+=contents.charAt(i);
            }
            precision_x_scale.setTextContent(t);
            x_scale.appendChild(precision_x_scale);
            root.appendChild(x_scale);

            Element y_scale=document.createElement("scale");
            Element col_y_scale=document.createElement("col_y_scale");
            t="";
            i+=36;
            for (;;i++)
            {
                if (contents.substring(i,i+6).compareTo("</col>")==0)
                    break;
                t+=contents.charAt(i);
            }
            col_y_scale.setTextContent(t);
            y_scale.appendChild(col_y_scale);
            Element wid_y_scale=document.createElement("wid_y_scale");
            t="";
            i+=12;
            for (;;i++)
            {
                if (contents.substring(i,i+6).compareTo("</wid>")==0)
                    break;
                t+=contents.charAt(i);
            }
            wid_y_scale.setTextContent(t);
            y_scale.appendChild(wid_y_scale);
            Element direction_y_scale=document.createElement("direction_y_scale");
            t="";
            i+=18;
            for (;;i++)
            {
                if (contents.substring(i,i+12).compareTo("</direction>")==0)
                    break;
                t+=contents.charAt(i);
            }
            direction_y_scale.setTextContent(t);
            y_scale.appendChild(direction_y_scale);
            Element pos_y_scale=document.createElement("pos_y_scale");
            t="";
            i+=18;
            for (;;i++)
            {
                if (contents.substring(i,i+6).compareTo("</pos>")==0)
                    break;
                t+=contents.charAt(i);
            }
            pos_y_scale.setTextContent(t);
            y_scale.appendChild(pos_y_scale);
            Element from_y_scale=document.createElement("from_y_scale");
            t="";
            i+=13;
            for (;;i++)
            {
                if (contents.substring(i,i+7).compareTo("</from>")==0)
                    break;
                t+=contents.charAt(i);
            }
            from_y_scale.setTextContent(t);
            y_scale.appendChild(from_y_scale);
            Element step_y_scale=document.createElement("step_y_scale");
            t="";
            i+=14;
            for (;;i++)
            {
                if (contents.substring(i,i+7).compareTo("</step>")==0)
                    break;
                t+=contents.charAt(i);
            }
            step_y_scale.setTextContent(t);
            y_scale.appendChild(step_y_scale);
            Element amount_y_scale=document.createElement("amount_y_scale");
            t="";
            i+=16;
            for (;;i++)
            {
                if (contents.substring(i,i+9).compareTo("</amount>")==0)
                    break;
                t+=contents.charAt(i);
            }
            amount_y_scale.setTextContent(t);
            y_scale.appendChild(amount_y_scale);
            Element precision_y_scale=document.createElement("precision_y_scale");
            t="";
            i+=21;
            for (;;i++)
            {
                if (contents.substring(i,i+12).compareTo("</precision>")==0)
                    break;
                t+=contents.charAt(i);
            }
            precision_y_scale.setTextContent(t);
            y_scale.appendChild(precision_y_scale);
            root.appendChild(y_scale);

            document.appendChild(root);
            TransformerFactory tff = TransformerFactory.newInstance();
            Transformer tf = tff.newTransformer();
            tf.setOutputProperty(OutputKeys.INDENT, "yes");
            tf.transform(new DOMSource(document), new StreamResult(new File("tmp.xml")));
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("生成xml文件失败");
        }
    }
    public static void read_xml() {
        try {
            DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
            DocumentBuilder db=factory.newDocumentBuilder();
            Document doc=db.parse("tmp.xml");
            NodeList list;
            list=doc.getElementsByTagName("bg");
            for (int i=0;i<list.getLength();i++) {
                org.w3c.dom.Node node=list.item(i);
                NodeList childNodes=node.getChildNodes();
                for (int j=0;j<childNodes.getLength();j++) {
                    if (childNodes.item(j).getNodeName().compareTo("col")==0) {
                        if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE)
                            mp.bg_col=childNodes.item(j).getTextContent();
                    }
                }
            }
            list=doc.getElementsByTagName("points");
            for (int i=0;i<list.getLength();i++) {
                org.w3c.dom.Node node=list.item(i);
                NodeList childNodes=node.getChildNodes();
                for (int j=0;j<childNodes.getLength();j++) {
                    if (childNodes.item(j).getNodeName().compareTo("pad")==0) {
                        if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE)
                            mp.points_pad=childNodes.item(j).getTextContent();
                    }
                    if (childNodes.item(j).getNodeName().compareTo("col")==0) {
                        if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE)
                            mp.points_col=childNodes.item(j).getTextContent();
                    }
                    if (childNodes.item(j).getNodeName().compareTo("wid")==0) {
                        if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE)
                            mp.points_wid=Double.parseDouble(childNodes.item(j).getTextContent());
                    }
                    if (childNodes.item(j).getNodeName().compareTo("radius")==0) {
                        if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE)
                            mp.points_radius=Double.parseDouble(childNodes.item(j).getTextContent());
                    }
                    if (childNodes.item(j).getNodeName().compareTo("list")==0) {
                        if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE)
                            mp.point_set=childNodes.item(j).getTextContent();
                    }
                }
            }
            list=doc.getElementsByTagName("lines");
            for (int i=0;i<list.getLength();i++) {
                org.w3c.dom.Node node=list.item(i);
                NodeList childNodes=node.getChildNodes();
                for (int j=0;j<childNodes.getLength();j++) {
                    if (childNodes.item(j).getNodeName().compareTo("col")==0) {
                        if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE)
                            mp.lines_col=childNodes.item(j).getTextContent();
                    }
                    if (childNodes.item(j).getNodeName().compareTo("wid")==0) {
                        if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE)
                            mp.lines_wid=Double.parseDouble(childNodes.item(j).getTextContent());
                    }
                    if (childNodes.item(j).getNodeName().compareTo("list")==0) {
                        if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE)
                            mp.lines_point_set=childNodes.item(j).getTextContent();
                    }
                }
            }
            list=doc.getElementsByTagName("line");
            for (int i=0;i<list.getLength();i++) {
                org.w3c.dom.Node node=list.item(i);
                NodeList childNodes=node.getChildNodes();
                for (int j=0;j<childNodes.getLength();j++) {
                    if (childNodes.item(j).getNodeName().compareTo("col")==0) {
                        if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE)
                            mp.line_col=childNodes.item(j).getTextContent();
                    }
                    if (childNodes.item(j).getNodeName().compareTo("wid")==0) {
                        if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE)
                            mp.line_wid=Double.parseDouble(childNodes.item(j).getTextContent());
                    }
                    if (childNodes.item(j).getNodeName().compareTo("point")==0) {
                        if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE)
                            mp.line_point=childNodes.item(j).getTextContent();
                    }
                    if (childNodes.item(j).getNodeName().compareTo("slope")==0) {
                        if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE)
                            mp.line_slope=childNodes.item(j).getTextContent();
                    }
                }
            }
            list=doc.getElementsByTagName("curve");
            for (int i=0;i<list.getLength();i++) {
                org.w3c.dom.Node node=list.item(i);
                NodeList childNodes=node.getChildNodes();
                for (int j=0;j<childNodes.getLength();j++) {
                    if (childNodes.item(j).getNodeName().compareTo("col")==0) {
                        if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE)
                            mp.curve_col=childNodes.item(j).getTextContent();
                    }
                    if (childNodes.item(j).getNodeName().compareTo("wid")==0) {
                        if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE)
                            mp.curve_wid=Double.parseDouble(childNodes.item(j).getTextContent());
                    }
                    if (childNodes.item(j).getNodeName().compareTo("range")==0) {
                        if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE)
                            mp.curve_range=childNodes.item(j).getTextContent();
                    }
                    if (childNodes.item(j).getNodeName().compareTo("amount")==0) {
                        if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE)
                            mp.curve_amount=Double.parseDouble(childNodes.item(j).getTextContent());
                    }
                    if (childNodes.item(j).getNodeName().compareTo("function")==0) {
                        if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE)
                            mp.curve_function=childNodes.item(j).getTextContent();
                    }
                }
            }
            list=doc.getElementsByTagName("shape");
            for (int i=0;i<list.getLength();i++) {
                org.w3c.dom.Node node=list.item(i);
                NodeList childNodes=node.getChildNodes();
                for (int j=0;j<childNodes.getLength();j++) {
                    if (childNodes.item(j).getNodeName().compareTo("col_oval")==0) {
                        if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE)
                            mp.oval_col=childNodes.item(j).getTextContent();
                    }
                    if (childNodes.item(j).getNodeName().compareTo("type_oval")==0) {
                        if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE)
                            mp.oval_type=childNodes.item(j).getTextContent();
                    }
                    if (childNodes.item(j).getNodeName().compareTo("pad_oval")==0) {
                        if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE)
                            mp.oval_pad=childNodes.item(j).getTextContent();
                    }
                    if (childNodes.item(j).getNodeName().compareTo("center_oval")==0) {
                        if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE)
                            mp.oval_center=childNodes.item(j).getTextContent();
                    }
                    if (childNodes.item(j).getNodeName().compareTo("wid_oval")==0) {
                        if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE)
                            mp.oval_wid=Double.parseDouble(childNodes.item(j).getTextContent());
                    }
                    if (childNodes.item(j).getNodeName().compareTo("width_oval")==0) {
                        if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE)
                            mp.oval_width=Double.parseDouble(childNodes.item(j).getTextContent());
                    }
                    if (childNodes.item(j).getNodeName().compareTo("height_oval")==0) {
                        if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE)
                            mp.oval_height=Double.parseDouble(childNodes.item(j).getTextContent());
                    }
                    if (childNodes.item(j).getNodeName().compareTo("col_rect")==0) {
                        if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE)
                            mp.rect_col=childNodes.item(j).getTextContent();
                    }
                    if (childNodes.item(j).getNodeName().compareTo("type_rect")==0) {
                        if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE)
                            mp.rect_type=childNodes.item(j).getTextContent();
                    }
                    if (childNodes.item(j).getNodeName().compareTo("pad_rect")==0) {
                        if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE)
                            mp.rect_pad=childNodes.item(j).getTextContent();
                    }
                    if (childNodes.item(j).getNodeName().compareTo("center_rect")==0) {
                        if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE)
                            mp.rect_center=childNodes.item(j).getTextContent();
                    }
                    if (childNodes.item(j).getNodeName().compareTo("wid_rect")==0) {
                        if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE)
                            mp.rect_wid=Double.parseDouble(childNodes.item(j).getTextContent());
                    }
                    if (childNodes.item(j).getNodeName().compareTo("width_rect")==0) {
                        if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE)
                            mp.rect_width=Double.parseDouble(childNodes.item(j).getTextContent());
                    }
                    if (childNodes.item(j).getNodeName().compareTo("height_rect")==0) {
                        if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE)
                            mp.rect_height=Double.parseDouble(childNodes.item(j).getTextContent());
                    }
                }
            }
            list=doc.getElementsByTagName("scale");
            for (int i=0;i<list.getLength();i++) {
                org.w3c.dom.Node node=list.item(i);
                NodeList childNodes=node.getChildNodes();
                for (int j=0;j<childNodes.getLength();j++) {
                    if (childNodes.item(j).getNodeName().compareTo("from_x_scale")==0) {
                        if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE)
                            mp.from_x_scale=Double.parseDouble(childNodes.item(j).getTextContent());
                    }
                    if (childNodes.item(j).getNodeName().compareTo("step_x_scale")==0) {
                        if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE)
                            mp.step_x_scale=Double.parseDouble(childNodes.item(j).getTextContent());
                    }
                    if (childNodes.item(j).getNodeName().compareTo("amount_x_scale")==0) {
                        if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE)
                            mp.amount_x_scale=Double.parseDouble(childNodes.item(j).getTextContent());
                    }
                    if (childNodes.item(j).getNodeName().compareTo("precision_x_scale")==0) {
                        if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE)
                            mp.precision_x_scale=Double.parseDouble(childNodes.item(j).getTextContent());
                    }
                    if (childNodes.item(j).getNodeName().compareTo("from_y_scale")==0) {
                        if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE)
                            mp.from_y_scale=Double.parseDouble(childNodes.item(j).getTextContent());
                    }
                    if (childNodes.item(j).getNodeName().compareTo("step_y_scale")==0) {
                        if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE)
                            mp.step_y_scale=Double.parseDouble(childNodes.item(j).getTextContent());
                    }
                    if (childNodes.item(j).getNodeName().compareTo("amount_y_scale")==0) {
                        if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE)
                            mp.amount_y_scale=Double.parseDouble(childNodes.item(j).getTextContent());
                    }
                    if (childNodes.item(j).getNodeName().compareTo("precision_y_scale")==0) {
                        if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE)
                            mp.precision_y_scale=Double.parseDouble(childNodes.item(j).getTextContent());
                    }
                }
            }
        } catch (Exception e) {
            System.out.println("文件打开失败！");
        }
    }
}
class MyPanel extends JPanel {
    public String bg_col="";
    public String points_col="";
    public String points_pad="";
    public String point_set="";
    public double points_wid=0;
    public double points_radius=0;
    public String lines_col="";
    public String lines_point_set="";
    public double lines_wid=0;
    public String line_col="";
    public double line_wid=0;
    public String line_point="";
    public String line_slope="";
    public double from_x_scale=0;
    public double from_y_scale=0;
    public double amount_x_scale=0;
    public double amount_y_scale=0;
    public double step_x_scale=0;
    public double step_y_scale=0;
    public double precision_x_scale=0;
    public double precision_y_scale=0;
    public String oval_col="";
    public String oval_type="";
    public String oval_pad="";
    public String oval_center="";
    public double oval_wid=0;
    public double oval_width=0;
    public double oval_height=0;
    public String rect_col="";
    public String rect_type="";
    public String rect_pad="";
    public String rect_center="";
    public double rect_wid=0;
    public double rect_width=0;
    public double rect_height=0;
    public String curve_col="";
    public double curve_wid=0;
    public String curve_range="";
    public double curve_amount=0;
    public String curve_function="";
    public double cprex=0;
    public double cprey=0;
    public Color setcolor(String col) {
        Color color=null;
        int f=0;
        for (int i=0;i<col.length();i++) {
            if (col.charAt(i)==',') {
                f=1;
                break;
            }
        }
        int cr=0,cg=0,cb=0;
        if (f==1) {
            int cnt=0;
            String t="";
            for (int i=0;i<col.length();i++) {
                if (col.charAt(i)!=',') {
                    t+=col.charAt(i);
                }
                else if (cnt==0&&col.charAt(i)==',') {
                    cr=(int)Double.parseDouble(t);
                    t="";
                    cnt=1;
                }
                else {
                    cg=(int)Double.parseDouble(t);
                    t="";
                }
            }
            cb=(int)Double.parseDouble(t);
            //System.out.println(cr+" "+cg+" "+cb);
            color=new Color(cr,cg,cb);
        }
        else {
            if (col.compareTo(" green ")==0)
                color=Color.GREEN;
            else if (col.compareTo(" black ")==0)
                color=Color.BLACK;
            else if (col==" cyan ")
                color=Color.CYAN;
            else if (col==" gray ")
                color=Color.GRAY;
            else if (col.compareTo(" orange ")==0)
                color=Color.ORANGE;
            else if (col.compareTo(" pink ")==0)
                color=Color.PINK;
            else if (col.compareTo(" red ")==0)
                color=Color.RED;
            else if (col.compareTo(" white ")==0)
                color=Color.WHITE;
            else if (col.compareTo(" yellow ")==0)
                color=Color.YELLOW;
            else
                color=Color.BLACK;
        }
        return color;
    }
//    public void function(double x,String func,Graphics2D g2,int num) {
//        int i=0,j=0;
//        String t="";
//        String f_func="";
//        int f=0;
//        if (x<0)
//            f=1;
//        while (i<func.length()) {
//            if (func.charAt(i)=='M') {
//                j=i+11;
//                String xp="";
//                while (func.charAt(j)!=')') {
//                    xp+=func.charAt(j);
//                    j++;
//                }
//                double d=Double.parseDouble(xp);
//                String c=String.valueOf(x);
//                f_func+=c;
//                for (int z=1;z<(int)d;z++) {
//                    f_func+="*"+c;
//                }
//                i+=j+1;
//            }
//            else if (func.charAt(i)=='x') {
//                String c=String.valueOf(x);
//                f_func+=c;
//                i+=c.length();
//            }
//            else {
//                f_func+=func.charAt(i);
//                i++;
//            }
//
//        }
//        System.out.println(f_func);
//        i=0;
//        j=0;
//        ArrayList<String> s1=new ArrayList<>();
//        while (i<f_func.length()) {
//            if (f_func.charAt(i)=='+'||f_func.charAt(i)=='-'||f_func.charAt(i)=='*'||f_func.charAt(i)=='/') {
//                for (;j<i;j++) {
//                    t+=f_func.charAt(j);
//                }
//                s1.add(t);
//                t="";
//                j++;
//                s1.add(String.valueOf(f_func.charAt(i)));
//            }
//            i++;
//        }
//        for (;j<f_func.length();j++) {
//            t+=f_func.charAt(j);
//        }
//        s1.add(t);
//        System.out.println(s1);
//        Stack<String> opnd=new Stack<>();
//        Stack<String> optr=new Stack<>();
//        optr.push("#");
//        int p=0;
//        while (p<s1.size()||optr.peek()!="#") {
//            try {
//                if (In(s1.get(p))!=1) {
//                    opnd.push(s1.get(p));
//                    p++;
//                }
//                else {
//                    try {
//                        switch (Precede(optr.peek(),s1.get(p))) {
//                            case '<':
//                                optr.push(s1.get(p));
//                                p++;
//                                break;
//                            case '=':
//                                optr.pop();
//                                p++;
//                                break;
//                            case '>':
//                                String xx=optr.peek();
//                                optr.pop();
//                                String a=opnd.peek();
//                                opnd.pop();
//                                String b=opnd.peek();
//                                opnd.pop();
//                                opnd.push(Operate(a,xx,b,x));
//                                break;
//                        }
//                    } catch (EmptyStackException e) {
//                        break;
//                    }
//                }
//            } catch (IndexOutOfBoundsException e) {
//                break;
//            }
//        }
//        String xx=optr.peek();
//        optr.pop();
//        String a=opnd.peek();
//        opnd.pop();
//        String b=opnd.peek();
//        opnd.pop();
//        opnd.push(Operate(b,xx,a,x));
//        double y=Double.parseDouble(opnd.peek());
//        double r=curve_wid;
//        if (num==0) {
//            num=1;
//            cprex=x;
//            cprey=y;
//        }
//        else {
//            Line2D l=new Line2D.Double();
//            l.setLine(200+50*(cprex-from_x_scale)/step_x_scale-5,700-50*(cprey-from_y_scale)/step_y_scale,200+50*(x-from_x_scale)/step_x_scale-5,700-50*(y-from_y_scale)/step_y_scale);
//            g2.setPaint(setcolor(curve_col));
//            BasicStroke bs=new BasicStroke((float) curve_wid,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);
//            g2.setStroke(bs);
//            g2.draw(l);
//            cprex=x;
//            cprey=y;
//        }
//    }
//    public int In(String c) {
//        if(c.compareTo("+")==0||c.compareTo("-")==0||c.compareTo("*")==0||c.compareTo("/")==0||c.compareTo("#")==0||c.compareTo("(")==0||c.compareTo(")")==0)
//            return 1;
//        else
//            return 0;
//    }
//    public char Precede(String a,String b) {
//        if(a.compareTo("+")==0||a.compareTo("-")==0)
//        {
//            if(b.compareTo("+")==0||b.compareTo("-")==0||b.compareTo(")")==0||b.compareTo("#")==0)
//                return '>';
//            else
//                return '<';
//        }
//        if(a.compareTo("*")==0||a.compareTo("/")==0)
//        {
//            if(b.compareTo("(")==0)
//                return '<';
//            else
//                return '>';
//        }
//        if(a.compareTo("(")==0){
//            if(b.compareTo(")")==0)
//                return '=';
//            else
//                return '<';
//        }
//        if(a.compareTo("#")==0)
//        {
//            if(b.compareTo("#")==0)
//                return '=';
//            else
//                return '<';
//        }
//        return '=';
//    }
//    public String Operate(String a,String x,String b,double num)
//    {
//        switch (x)
//        {
//            case "+":
//                return String.valueOf(Double.parseDouble(a)+Double.parseDouble(b));
//            case "-":
//                return String.valueOf(Double.parseDouble(a)-Double.parseDouble(b));
//            case "*":
//                return String.valueOf(Double.parseDouble(a.trim())*Double.parseDouble(b.trim()));
//            case "/":
//                return String.valueOf(Double.parseDouble(a)/Double.parseDouble(b));
//            default:
//                return "-1";
//        }
//    }
    public void function(float x,String func,Graphics2D g2,int mun) throws Exception {
        ArrayList<String> ass=new ArrayList<>();
        ArrayList<Character> cc=new ArrayList<>();
        String t="";
        for (int i=0;i<func.length();i++) {
            if (func.charAt(i)=='M') {
                for (int k=i;func.charAt(k)!=')';k++)
                    t+=func.charAt(k);
                t+=')';
                ass.add(t);
                i+=t.length()-1;
                t="";
            }
            else if (func.charAt(i)=='+'||(func.charAt(i)=='-'&&i!=0)) {
                cc.add(func.charAt(i));
                if (t!="")
                    ass.add(t);
                t="";
            }
            else
                t+=func.charAt(i);
        }
        if (t!=""&&t!=" ")
            ass.add(t);
        ass.remove("");
        ass.remove(" ");
        String num=String.valueOf(x);
        for (int i=0;i<ass.size();i++) {
            String s=ass.get(i);
            if (s.contains("x")) {
                int j=s.indexOf("x");
                String s1=s.substring(0,j);
                String s2=s.substring(j+1,s.length());
                String ss=s1+num+s2;
                ass.set(i,ss);
            }
            if (ass.get(i).contains("Math.pow")) {
                int idx=ass.get(i).indexOf(',');
                int v=idx-1;
                String aa="";
                String bb="";
                for (;ass.get(i).charAt(v)!='(';v--)
                    aa=ass.get(i).charAt(v)+aa;
                idx++;
                for (;ass.get(i).charAt(idx)!=')';idx++)
                    bb+=ass.get(i).charAt(idx);
                ass.set(i,pow(Double.parseDouble(aa),Double.parseDouble(bb)));
            }
            if (ass.get(i).contains("*")) {
                int idx=ass.get(i).indexOf('*');
                int v=idx-1;
                String aa="";
                String bb="";
                for (;v>=0;v--)
                    aa=ass.get(i).charAt(v)+aa;
                idx++;
                for (;idx<ass.get(i).length();idx++)
                    bb+=ass.get(i).charAt(idx);
                ass.set(i,mul(Double.parseDouble(aa),Double.parseDouble(bb)));
            }
            if (ass.get(i).contains("/")) {
                int idx=ass.get(i).indexOf('/');
                int v=idx-1;
                String aa="";
                String bb="";
                for (;v>=0;v--)
                    aa=ass.get(i).charAt(v)+aa;
                idx++;
                for (;idx<ass.get(i).length();idx++)
                    bb+=ass.get(i).charAt(idx);
                ass.set(i,div(Double.parseDouble(aa),Double.parseDouble(bb)));
            }
        }
        int p=0;
        int q=1;
        double y=0;
        for (;q<ass.size();q++) {
            if (cc.get(p).compareTo('+')==0)
                y+=Double.parseDouble(ass.get(q-1))+Double.parseDouble(ass.get(q));
            else
                y+=Double.parseDouble(ass.get(q-1))-Double.parseDouble(ass.get(q));
            p++;
        };
        if (mun==0) {
            mun=1;
            cprex=x;
            cprey=y;
        }
        else {
            Line2D l=new Line2D.Double();
            l.setLine(200+50*(cprex-from_x_scale)/step_x_scale-5,700-50*(cprey-from_y_scale)/step_y_scale,200+50*(x-from_x_scale)/step_x_scale-5,700-50*(y-from_y_scale)/step_y_scale);
            g2.setPaint(setcolor(curve_col));
            BasicStroke bs=new BasicStroke((float) curve_wid,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);
            g2.setStroke(bs);
            g2.draw(l);
            cprex=x;
            cprey=y;
        }
    }
    public String pow(double aa,double bb) throws Exception{
        String str="Math.pow(a,b)";
        Map<String, Object> input=new TreeMap<>();
        input.put("a",aa);
        input.put("b",bb);
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("javascript");
        return String.valueOf(engine.eval(str,new SimpleBindings(input)));
    }
    public String mul(double aa,double bb) throws Exception{
        String str="a*b";
        Map<String,Object> input=new TreeMap<>();
        input.put("a",aa);
        input.put("b",bb);
        ScriptEngineManager manager=new ScriptEngineManager();
        ScriptEngine engine=manager.getEngineByName("javascript");
        return String.valueOf(engine.eval(str,new SimpleBindings(input)));
    }
    public String div(double aa,double bb) throws Exception{
        String str="a/b";
        Map<String,Object> input=new TreeMap<>();
        input.put("a",aa);
        input.put("b",bb);
        ScriptEngineManager manager=new ScriptEngineManager();
        ScriptEngine engine=manager.getEngineByName("javascript");
        return String.valueOf(engine.eval(str,new SimpleBindings(input)));
    }
    @Override public void paintComponent(Graphics g) {
        Graphics2D g2=(Graphics2D)g;
        g2.setBackground(setcolor(bg_col));
        g2.clearRect(0, 0, (int)getBounds().getWidth(), (int)getBounds().getHeight());
        g2.drawLine(195,700,195,175);
        g2.drawLine(195,700,725,700);
        for (int i=0;i<=(int)amount_x_scale;i++) {
            BigDecimal bd = new BigDecimal(from_x_scale+i*step_x_scale/(amount_x_scale/10));
            bd = bd.setScale((int) precision_x_scale, RoundingMode.HALF_UP);
            g2.drawString(bd.toString(), 195+500/(int)amount_x_scale*i,715);
            if (i!=0)
                g2.drawLine(195+500/(int)amount_x_scale*i,700,195+500/(int)amount_x_scale*i,695);
        }
        for (int i=0;i<=(int)amount_y_scale;i++) {
            BigDecimal bd = new BigDecimal(from_y_scale+i*step_y_scale/(amount_y_scale/10));
            bd = bd.setScale((int) precision_y_scale, RoundingMode.HALF_UP);
            g2.drawString(bd.toString(), 160,700-500/(int)amount_y_scale*i);
            if (i!=0)
                g2.drawLine(195,700-500/(int)amount_y_scale*i,200,700-500/(int)amount_y_scale*i);
        }
        String t="";
        for (int i=1;i<point_set.length();i++) {

            if (point_set.charAt(i)!='\n') {
                t+=point_set.charAt(i);
            }
            else {
                int j=0;
                for (;j<t.length();j++) {
                    if (t.charAt(j)==',')
                        break;
                }
                String tx="";
                String ty="";
                for (int k=0;k<j;k++) {
                    tx+=t.charAt(k);
                }
                double px=Double.parseDouble(tx);
                for (int k=j+1;k<t.length();k++) {
                    ty+=t.charAt(k);
                }
                double py=Double.parseDouble(ty);
                Ellipse2D p=new Ellipse2D.Double();
                p.setFrameFromCenter(200+50*(px-from_x_scale)/step_x_scale-5,700-50*(py-from_y_scale)/step_y_scale,200+50*(px-from_x_scale)/step_x_scale,700-50*(py-from_y_scale)/step_y_scale+points_radius);
                g2.setPaint(setcolor(points_col));
                if (points_pad.compareTo(" false ")==0) {
                    BasicStroke bs=new BasicStroke((float) points_wid,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);
                    g2.setStroke(bs);
                }
                else
                    g2.fill(p);
                g2.draw(p);
                t="";
            }
        }
        t="";
        int num=0;
        double prex=0;
        double prey=0;
        for (int i=1;i<lines_point_set.length();i++) {
            if (lines_point_set.charAt(i)!='\n') {
                t+=lines_point_set.charAt(i);
            }
            else {
                int j=0;
                for (;j<t.length();j++) {
                    if (t.charAt(j)==',')
                        break;
                }
                String tx="";
                String ty="";
                for (int k=0;k<j;k++) {
                    tx+=t.charAt(k);
                }
                double px=Double.parseDouble(tx);
                for (int k=j+1;k<t.length();k++) {
                    ty+=t.charAt(k);
                }
                double py=Double.parseDouble(ty);
                //System.out.println(px+" "+py);

                if (num==0) {
                    num=1;
                    prex=px;
                    prey=py;
                }
                else {
                    Line2D l=new Line2D.Double();
                    l.setLine(200+50*(prex-from_x_scale)/step_x_scale-5,700-50*(prey-from_y_scale)/step_y_scale,200+50*(px-from_x_scale)/step_x_scale-5,700-50*(py-from_y_scale)/step_y_scale);
                    g2.setPaint(setcolor(lines_col));
                    BasicStroke bs=new BasicStroke((float) lines_wid,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);
                    g2.setStroke(bs);
                    g2.draw(l);
                    prex=px;
                    prey=py;
                }
                t="";
            }
        }
        t="";
        int i=0;
        for (;i<line_point.length();i++) {
            if (line_point.charAt(i)==',')
                break;
        }
        for (int j=0;j<i;j++) {
            t+=line_point.charAt(j);
        }
        double pcx=Double.parseDouble(t);
        t="";
        for (int j=i+1;j<line_point.length();j++) {
            t+=line_point.charAt(j);
        }
        double pcy=Double.parseDouble(t);
        pcx=200+50*(pcx-from_x_scale)/step_x_scale-5;
        pcy=700-50*(pcy-from_y_scale)/step_y_scale;
        if (line_slope.compareTo(" inf ")==0) {
            Line2D l1=new Line2D.Double();
            l1.setLine(pcx,pcy,pcx,-1);
            g2.setColor(setcolor(line_col));
            BasicStroke bs=new BasicStroke((float) line_wid,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);
            g2.setStroke(bs);
            g2.draw(l1);
            Line2D l2=new Line2D.Double();
            l2.setLine(pcx,pcy,pcx,5000);
            g2.setColor(setcolor(line_col));
            g2.setStroke(bs);
            g2.draw(l2);
        }
        else {
            double k=(-1)*Double.parseDouble(line_slope)*step_x_scale/step_y_scale;
            Line2D l1=new Line2D.Double();
            l1.setLine(pcx,pcy,3000,k*(3000-pcx)+pcy);
            g2.setColor(setcolor(line_col));
            BasicStroke bs=new BasicStroke((float) line_wid,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);
            g2.setStroke(bs);
            g2.draw(l1);
            Line2D l2=new Line2D.Double();
            l2.setLine(pcx,pcy,-1,k*(-1-pcx)+pcy);
            g2.setStroke(bs);
            g2.draw(l2);
        }
        t="";
        i=0;
        for (;i<curve_range.length();i++) {
            if (curve_range.charAt(i)==',')
                break;
        }
        for (int j=0;j<i;j++)
            t+=curve_range.charAt(j);
        float l=Float.parseFloat(t);
        t="";
        for (int j=i+1;j<curve_range.length();j++)
            t+=curve_range.charAt(j);
        float r=Float.parseFloat(t);
        float step= (float) ((r-l)/curve_amount);
        int mun=0;
        for (float j=l;j<=r;j=j+step) {
            try {
                function(j,curve_function,g2,mun++);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        t="";
        i=0;
        for (;i<oval_center.length();i++) {
            if (oval_center.charAt(i)==',')
                break;
        }
        for (int j=0;j<i;j++) {
            t+=oval_center.charAt(j);
        }
        double cx=Double.parseDouble(t);
        t="";
        for (int j=i+1;j<oval_center.length();j++) {
            t+=oval_center.charAt(j);
        }
        double cy=Double.parseDouble(t);
        if (oval_type.compareTo(" oval ")==0) {
            Ellipse2D e=new Ellipse2D.Double();
            e.setFrameFromCenter(200+50*(cx-from_x_scale)/step_x_scale,700-50*(cy-from_y_scale)/step_y_scale,200+50*(cx-from_x_scale)/step_x_scale+oval_width/step_x_scale*25,700-50*(cy-from_y_scale)/step_y_scale+oval_height/step_y_scale*25);
            g2.setPaint(setcolor(oval_col));
            if (oval_pad.compareTo(" true ")==0)
                g2.fill(e);
            else {
                BasicStroke bs=new BasicStroke((float) oval_wid,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);
                g2.setStroke(bs);
            }
            g2.draw(e);
        }
        else {
            Rectangle2D e=new Rectangle.Double();
            e.setFrameFromCenter(200+50*(cx-from_x_scale)/step_x_scale,700-50*(cy-from_y_scale)/step_y_scale,200+50*(cx-from_x_scale)/step_x_scale+oval_width/step_x_scale*25,700-50*(cy-from_y_scale)/step_y_scale+oval_height/step_y_scale*25);
            g2.setPaint(setcolor(oval_col));
            if (oval_pad.compareTo(" true ")==0)
                g2.fill(e);
            else {
                BasicStroke bs=new BasicStroke((float) oval_wid,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);
                g2.setStroke(bs);
            }
            g2.draw(e);
        }

        t="";
        i=0;
        for (;i<rect_center.length();i++) {
            if (rect_center.charAt(i)==',')
                break;
        }
        for (int j=0;j<i;j++) {
            t+=rect_center.charAt(j);
        }
        cx=Double.parseDouble(t);
        t="";
        for (int j=i+1;j<rect_center.length();j++) {
            t+=rect_center.charAt(j);
        }
        cy=Double.parseDouble(t);
        if (rect_type.compareTo(" oval ")==0) {
            Ellipse2D e=new Ellipse2D.Double();
            e.setFrameFromCenter(200+50*(cx-from_x_scale)/step_x_scale,700-50*(cy-from_y_scale)/step_y_scale,200+50*(cx-from_x_scale)/step_x_scale+rect_width/step_x_scale*25,700-50*(cy-from_y_scale)/step_y_scale+rect_height/step_y_scale*25);
            g2.setPaint(setcolor(rect_col));
            if (rect_pad.compareTo(" true ")==0)
                g2.fill(e);
            else {
                BasicStroke bs=new BasicStroke((float) rect_wid,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);
                g2.setStroke(bs);
            }
            g2.draw(e);
        }
        else {
            Rectangle2D e=new Rectangle.Double();
            e.setFrameFromCenter(200+50*(cx-from_x_scale)/step_x_scale,700-50*(cy-from_y_scale)/step_y_scale,200+50*(cx-from_x_scale)/step_x_scale+rect_width/step_x_scale*25,700-50*(cy-from_y_scale)/step_y_scale+rect_height/step_y_scale*25);
            g2.setPaint(setcolor(rect_col));
            if (rect_pad.compareTo(" true ")==0)
                g2.fill(e);
            else {
                BasicStroke bs=new BasicStroke((float) rect_wid,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);
                g2.setStroke(bs);
            }
            g2.draw(e);
        }
    }
}